(function(){
    var chk_package_ticket = function() {
        if ($("#RecommendProducts").length) {
            $("#RecommendProducts").remove();
            $("#pt-err-msg").remove();
        }
        var $tbTickets = $("#tbTickets"),
            width       = 160,
            style_width = 0,
            ticketsHtml = '',
            data        = {
                token     : "<?=$token?>",
                appid     : "<?=$appid?>",
                signature : "<?=md5($token.$appid.$pid);?>",
                pid       : "<?=$pid?>",
                aid       : "<?=$aid?>",
                tnum      : $("#purchaseNum").val(),
                beginTime : $("#begintime").val(),
                showTicket: 1
            };
        $.getJSON('/module/link_product/index.php', data)
            .done(function(res){
                if (res['code']!=200) {
                    $(".submitBtn").hide();
                }  else {
                    $(".submitBtn").show();
                }
                if (res['code']>200 && res['code']<909) {
                    $tbTickets.after('<div id="pt-err-msg">'+res['msg']+'</div>');
                    return false;
                } else {

                }
                var cnt = 0, len = res['list'].length,tickets = res['list'],
                    msg_index = -1;
                for(var i in tickets) {
                    cnt += 1;

                    if (tickets[i]['msg'].length>0) {
                        msg_index = i;
                    }

                    ticketsHtml += '<li><p><img src="'+tickets[i]['l_img']+'"/></p>'
                        + tickets[i]['l_title']+'<br/>'+tickets[i]['t_title']
                        + 'X'+tickets[i]['num']+'(张)'
                        + tickets[i]['msg']
                    '</li>';
                    if (cnt != len) {
                        ticketsHtml += '<li class="plus">+</li>';
                    }
                    style_width += width;
                }
                var html = '<div id="RecommendProducts" class="tickets">' +
                    '<p class="cld-head-title">包含票类：</p>' +
                    '<ul id="link_tiicketUl" class="link_tickets" style="width:'
                    +style_width+'px;">'+ticketsHtml+'</ul></div>';
                if (msg_index>-1) {
                    html += '<div id="pt-err-msg">抱歉，有子票'+tickets[msg_index]['m']+'，无法提交订单。</div>';
                }
                $tbTickets.after(html);
            });
    };
    chk_package_ticket();
    var id = 0;
    $("#purchaseNum").on('change', function(){
        console.log(id);
        if (id>0) {
            clearTimeout(id);
        }
        id = setTimeout(chk_package_ticket, 1200);
    });
    $("#begintime").on('change', chk_package_ticket);
})();


var ChildTicketSlide = (function(){
		var animating = false;
		var onMousewheelEvent = function(target,callback){
				target = typeof target == "string" ? document.getElementById(target) : target;
				if (document.attachEvent) {
					target.attachEvent("onmousewheel", function(event) {	
						event.delta = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3;
						callback(event.delta);
						event.preventDefault();
						window.vent.cancelBubble = true;
						event.stopPropagation();
					});
				}else if (document.addEventListener) {
					target.addEventListener("DOMMouseScroll", function(event) {
						event.delta = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3;
						callback(event.delta);
						event.preventDefault();
						event.stopPropagation();
					}, false);
				}
		};
		var getStep = function(ele){
				return 168;
				//return  ele.find("li").first().outerWidth() + ele.find(".plus").outerWidth();
		};
		var dir = 0, _timer,  _move = function(ele, dir) {
				var p = ele.parent();
				var pw = p.width(), bw = ele.width();
				var step = getStep(ele);
				console.log(step)
				var s = dir * step;
				var limit = function(s){
						if(s>0){
								s = 0;
						}else if(Math.abs(s)>=Math.abs(pw-bw)){
							    s =pw-bw - 20;
						}
						return s;
				};
				s = limit(s);
				animating = true;
				ele.animate({"left" : s},"fast",function(){
						animating = false;
				})
		};
		var S={};
		S.init = function(){
			onMousewheelEvent(document.getElementById("link_tiicketUl"),function(delta){
				if(animating) return;
				 if(delta>0){
						dir++;
				 }else if(delta<0){
						dir--;
				 }
				 _move($("#link_tiicketUl"),dir);
			})
			
			
		}
		return S;
})()
